package battle 
{
	import Common.GameDefine;
	
	import flash.display.MovieClip;
	
	import map.MapGrid;
	/**
	 * ...
	 * @author 
	 */
	public class BattleHandlerReady extends BattleHandlerInterface
	{
		public const POS:int = 4;
		
		private var ready:int = 0;
		
		public function BattleHandlerReady() 
		{
			
		}
		
		override public function clear():void 
		{
			ready = 0;
			super.clear();
		}
		
		private function onMoveEnd( c:BattleCreature ):void
		{
			ready++;
			c.ready = true;
			
			if ( ready == BattleData.instance.defenderArray.length + BattleData.instance.playerArray.length )
			{
				BattleSession.instance.setState( BattleState.COMMAND );
			}
		}
		
		override public function active():void 
		{
			ready = 0;

			var fightMC:MovieClip = BattleData.instance.fightMC;
			
			for (var i:int = BattleData.instance.playerArray.length - 1 ; i >= 0 ; i-- ) 
			{
				if ( !BattleData.instance.playerArray[ i ].ready ) 
				{
					BattleData.instance.playerArray[ i ].setPos( fightMC.playerArray[ i ].x + MapGrid.gridX * POS , fightMC.playerArray[ i ].y + MapGrid.gridY * POS );
					BattleData.instance.playerArray[ i ].move( fightMC.playerArray[ i ].x , fightMC.playerArray[ i ].y , onMoveEnd );
				}
			}
			
			for ( i = BattleData.instance.playerPetArray.length - 1 ; i >= 0 ; i-- ) 
			{
				if ( !BattleData.instance.playerPetArray[ i ].ready ) 
				{
					BattleData.instance.playerPetArray[ i ].setPos( fightMC.playerPetArray[ i ].x + MapGrid.gridX * POS , fightMC.playerPetArray[ i ].y + MapGrid.gridY * POS );
					BattleData.instance.playerPetArray[ i ].move( fightMC.playerPetArray[ i ].x , fightMC.playerPetArray[ i ].y , onMoveEnd );
				}
			}
			
			for ( i = BattleData.instance.defenderArray.length - 1 ; i >= 0 ; i-- ) 
			{
				if ( !BattleData.instance.defenderArray[ i ].ready ) 
				{
					BattleData.instance.defenderArray[ i ].setPos( fightMC.enemyArray[ i ].x - MapGrid.gridX * POS , fightMC.enemyArray[ i ].y - MapGrid.gridY * POS );
					BattleData.instance.defenderArray[ i ].move( fightMC.enemyArray[ i ].x , fightMC.enemyArray[ i ].y , onMoveEnd );
				}
			}
			
			for ( i = BattleData.instance.defenderPetArray.length - 1 ; i >= 0 ; i-- ) 
			{
				if ( !BattleData.instance.defenderPetArray[ i ].ready ) 
				{
					BattleData.instance.defenderPetArray[ i ].setPos( fightMC.enemyPetArray[ i ].x - MapGrid.gridX * POS , fightMC.enemyPetArray[ i ].y - MapGrid.gridY * POS );
					BattleData.instance.defenderPetArray[ i ].move( fightMC.enemyPetArray[ i ].x , fightMC.enemyPetArray[ i ].y , onMoveEnd );
				}
			}
			
		}
		
		
	}

}